﻿#region Copyright (c) 2008 plexBB Team

/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

#endregion

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlexBB.Data;

namespace PlexBB.Controls.Admin.UserManagement
{
    public abstract class AcpUserOverview : AcpUserControlBase
    {
        #region Controls

        protected IButtonControl bSubmit;
        protected Control cInactiveUser;
        protected Control cSwitchPermissions;
        protected Control cUserIP;
        protected HyperLink hShowIP;
        protected HyperLink hSwitchPermissions;
        protected HyperLink hWhoIS;
        protected ListControl sUserFounder;
        protected ITextControl tConfirmEmail;
        protected ITextControl tNewPassword;
        protected ITextControl tPasswordConfirm;
        protected ITextControl tUserEmail;
        protected ITextControl tUserLastActive;
        protected ITextControl tUserName;
        protected ITextControl tUserNameCharsExplain;
        protected ITextControl tUserPosts;
        protected ITextControl tUserRegistered;
        protected ITextControl tUserWarnings;
        protected ITextControl tUserInactiveReason;

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                tUserName.Text = ManagedUser.UserName;
                sUserFounder.Text = (ManagedUser.UserType == UserType.UserFounder).ToString();
                cInactiveUser.Visible = ManagedUser.UserType == UserType.UserInactive;
                tUserRegistered.Text = User.FormatDate(ManagedUser.RegisteredDate);
                tUserLastActive.Text = ManagedUser.LastVisit.HasValue ? User.FormatDate(ManagedUser.LastVisit.Value) : "-";
                tUserPosts.Text = ManagedUser.Posts.ToString();
                tUserWarnings.Text = ManagedUser.Warnings.ToString();
                tUserEmail.Text = ManagedUser.Email;
            }

            bSubmit.Click += bSubmit_Click;
        }

        private void bSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                ManagedUser.UserName = tUserName.Text.Trim();

                // do not allow to change UserType for ignored or inactive users
                if (ManagedUser.UserType == UserType.UserFounder || ManagedUser.UserType == UserType.UserNormal)
                {
                    ManagedUser.UserType = bool.Parse(sUserFounder.Text) ? UserType.UserFounder : UserType.UserNormal;
                }
                ManagedUser.Email = tUserEmail.Text.Trim();
                if (tNewPassword.Text.Length > 0)
                {
                    ManagedUser.SetPassword(tNewPassword.Text);
                }
                Data.SubmitChanges();

                ShowMessage(null, Lang["USER_OVERVIEW_UPDATED"], CreateUrl(), true, true);
            }
        }
    }
}